<?php

/**
 * @author      :   Nhanva
 * @name        :   Author
 * @copyright   :   Fpt Online
 * @todo        :   Author model
 */
class Author {

    protected static $_instance = null;
    protected $_menu;
    protected $_section;
    protected $_cate = null;

    const AUTHOR_DETAIL = 'author_detail';

    /**
     * Constructor of class
     * we don't permit an explicit call of the constructor! (like $v = new Singleton())
     */
    protected function __construct() {
        
    }

    /**
     * Get singletom instance
     * @return <object>
     */
    public final static function getInstance() {
        //Check instance
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }

        //Return instance
        return self::$_instance;
    }
    
    /**
     * Get author info detail
     * @param int $intAuthorID
     * @return <array> Author information 
     */
    public function getDetailAuthor($intAuthorID) {
        try {
            // Get instance memcache
            $memcacheInstance = Fpt_Global::getMemcacheInstance($intAuthorID);
            $keyCache = Fpt_Global::makeCacheKey(self::AUTHOR_DETAIL, $intAuthorID);

            // Get author from Memcache
            $arrAuthorInfo = $memcacheInstance->read($keyCache);

            if (empty($arrAuthorInfo)) {
                //get application config
                $config = Fpt_Global::getApplicationIni();

                //get instance mysql
                $authorMysql = Fpt_Business_Author::getInstance($config['database']['default']);

                // Get data in DB
                $arrAuthorInfo = $authorMysql->getDetailAuthor($intAuthorID);

                if (!empty($arrAuthorInfo)) {
                    // Store category info to cache
                    $result = $memcacheInstance->write($keyCache, $arrAuthorInfo);

                    if ($result === false) {
                        Fpt_Log::sendlog(4, -1, 'memcache author', $intAuthorID, null, null);
                    }
                }
            }
        } catch (Exception $ex) {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        // Return author info
        return $arrAuthorInfo;
    }

    
}